package com.example.serviceworkorder.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.common.util.R;
import com.example.serviceworkorder.pojo.Project;
import com.example.serviceworkorder.pojo.Workorder;
import com.example.serviceworkorder.service.WorkOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/Service-WorkOrder/")
@Tag(name = "工单管理")
public class WorkOrderController {
    @Autowired
    private WorkOrderService workOrderService;

    @Operation(summary = "显示所有工单数据")
    @GetMapping(value = "/getAllWorkOrder")
    private R getAllWorkOrder(@RequestParam(required = false) Integer queryProjectId) {
        /*QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        if (queryProjectId != null && !queryProjectId.equals(-1)) {
            queryWrapper.eq("projectId", queryProjectId);
        }*/
        LambdaJoinQueryWrapper<Workorder> wrapper = new LambdaJoinQueryWrapper<>(Workorder.class);
        wrapper.innerJoin(Project.class, Project::getId, Workorder::getProjectId);
        wrapper.eq(Workorder::getProjectId,queryProjectId);
        List<Workorder> workorderList = workOrderService.list(wrapper);
        return R.ok().data("workorderList", workorderList);
    }

    @Operation(summary = "增加工单数据")
    @PostMapping(value = "/addWorkOrder")
    private R addWorkOrder(@RequestBody Workorder workOrder) {
        boolean flag = workOrderService.save(workOrder);
        if (flag) {
           return R.ok();
        } else {
           return R.error();
        }
    }

    @Operation(summary = "删除工单数据")
    @GetMapping(value = "/deleteWorkOrder/{id}")
    private R deleteWorkOrder(@PathVariable("id") Integer id) {
        boolean flag = workOrderService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Operation(summary = "按照编号工单数据")
    @GetMapping(value = "/getWorkOrderById/{id}")
    private R getWorkOrderById(@PathVariable("id") Integer id) {
       LambdaJoinQueryWrapper<Workorder> wrapper = new LambdaJoinQueryWrapper<>(Workorder.class);
       wrapper.innerJoin(Project.class, Project::getId, Workorder::getProjectId);
       wrapper.eq(Workorder::getId, id);
       Workorder workOrder = workOrderService.getOne(wrapper);
       if (workOrder != null) {
           return R.ok().data("workOrder", workOrder);
       } else {
           return R.error();
       }
    }

    @Operation(summary = "修改工单数据")
    @PostMapping(value = "updateWorkOrder")
    private R updateWorkOrder(@RequestBody Workorder workOrder) {
        boolean flag = workOrderService.updateById(workOrder);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}
